home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / select.zip / SELECTCO.ASM < prev    next >
Assembly Source File  |  1991-01-23  |  6KB  |  124 lines

  1. ;This ASM file when assembled with MASM and linked will make SELECT.EXE (the
  2. ;linker may give you a "no stack segment" warning but ignore it) and when
  3. ;converted with EXE2BIN.COM ( just run "EXE2BIN SELECT.EXE SELECT.COM" ) makes
  4. ;SELECT.COM which you invoke in a batch file as follows: "SELECT abcdefg"   
  5. ;where the chars that follow SELECT (the command tail) can be any combination
  6. ;of LOWER-CASE letters. Select then waits for you to type one of the letters on
  7. ;its command tail. If the key you press is not on SELECT's command tail, it
  8. ;beeps. If the key you press is on the command tail, select returns the
  9. ;alphabetical position of your letter  ( 'a' = 1 ). You can then use the IF
  10. ;ERRORLEVEL commands to check which letter was pressed. This is ideal for
  11. ;implementing menus in autoexec.bat or some other batch file. Don't run select
  12. ;without a command tail!
  13.  
  14. ;NOTE: ALL TEXT ON A LINE IN THIS ASM FILE FOLLOWING A SEMICOLON IS A COMMENT.
  15. ;MASM WILL IGNORE ALL COMMENTS AND THEY ARE THERE TO PROVIDE EXPLANATIONS TO
  16. ;THE CODE. YOU CAN ASSEMBLE FILE AS IS.
  17.  
  18.     name select          
  19.     page 55,78          
  20.     title SELECT.COM: Allows user to choose from alphabetical menu
  21.  
  22. ;The above section (name, page, and title) is only useful if you want to create
  23. ;a listing file (SELECT.LST) of the program (to create a listing file type the
  24. ;name of the program when MASM prompts you: [NUL.lst]).
  25.  
  26. cr    equ    0dh      ;calling 13 'cr' (carriage return is ASCII 13 or 0Dh)
  27. lf    equ    0ah      ;calling 10 'lf' (line feed is ASCII 10 or 0Ah [hex])
  28. alert    equ    07h      ;calling 7 'alert' (displaying ASCII 7 sounds a beep)
  29.  
  30. _TEXT    segment word public 'CODE'  ;Defines the following as code segment.
  31.                                      ;for now you don't need to know what 
  32.                                      ;'word' and 'public' mean but include them
  33.  
  34.     org    100h   ;.COM files always begin at 100h
  35.  
  36.     assume    cs:_TEXT,ds:_TEXT,ss:_TEXT
  37.  
  38.             ;The assume directive 
  39.             ;simply tells MASM where the segment registers 
  40.               ;should be pointing. We don't plan to use ES so
  41.             ;no need to specify it in an assume directive
  42.  
  43. select    proc    near       ;select defined as a 'near' procedure
  44.  
  45.     mov    di,0080h   ;0080h is the location of the 'command tail' i.e.
  46.                ;the other info you type on the DOS line after
  47.                ;the name of the program. eg. in 'DEL *.com', DEL
  48.                ;is the name of the program and '*.com' is the tail
  49.     mov    cl,[di]    ;move the first item of the command tail (which is
  50.                ;the length) into cl so you know how many chars to
  51.                ;read.
  52.     mov    bx,0081h   ;now bx gets the start of the actual characters
  53.                ;of the command tail.
  54.  
  55. ;The following loop (lab1) keeps checking the command tail until it reaches
  56. ;the 1st valid char, i.e. it skips superfluous initial spaces.
  57.  
  58. lab1:    mov    al,[bx]    ;al gets the next command tail character
  59.     cmp    al,'a'     ;validate the char (see if ASCII is less than 'a')
  60.     jnc    lab2       ;if valid escape this loop and go to lab2
  61.     dec    cl         ;decrease count
  62.     inc    bx         ;point to next char
  63.     jmp    lab1       ;go back (till you find a lowercase char)
  64.  
  65. lab2:    mov    valid,bx   ;record where valid command tail starts in bx
  66.     mov    lenval,cl  ;record the length of the valid comm. tail in cl
  67.  
  68.     mov    ah,9       ;DOS function 09h = write string to stdout
  69.     mov    dx,offset prompt  ;dx holds offset of string (see below)
  70.     int    21h        ;transfer to MS-DOS
  71.  
  72. lab3:    mov    ah,8       ;DOS function 09h = read 1 character from console
  73.     int    21h        ;transfer to MS-DOS
  74.     or    al,01000000b  ;if the input was upper-case, turn to lower
  75.     mov    ch,00      
  76.     mov    cl,lenval  ;cx contains the number of valid chars 
  77.     mov    bx,valid   ;bx points to the start of list of valid chars
  78.  
  79. lab4:    cmp    al,[bx]    ;check the input against this valid char
  80.     jz    found      ;if the same, exit this loop
  81.     inc    bx         ;if not point to the next valid char
  82.     loop    lab4       ;and try again... 
  83.  
  84.     mov    dl,alert   ;all valid chars checked but input is neither
  85.                   ;so ascii for "beep speaker" placed in dl
  86.     mov    ah,2       ;DOS function 02h = send character to console
  87.     int    21h        ;transfer to DOS (beep signals user of wrong input)
  88.     jmp    lab3       ;go back, get new input, and check it again
  89.  
  90. found:    mov    dl,al      ;place the entered character in dl
  91.     push    ax         ;save the ax reg for later
  92.     mov    ah,2       ;DOS function 02h = send character to console
  93.     int    21h        ;transfer to MS-DOS
  94.     mov    dl,cr      ;
  95.     mov    ah,2       ;
  96.     int    21h        ;now send a carriage return...
  97.     mov    dl,lf      ;
  98.     mov    ah,2       ;
  99.     int    21h        ;now send a line feed...   and we're dond
  100.     pop    ax         ;restore the ax register
  101.     sub    al,60h     ;96 decimal= 'a'-1. This reduces the char to it's
  102.                ;alphabetical position.
  103.     mov    ah,4ch     ;DOS function 4Ch = end process and return the 
  104.                ;errorlevel in al
  105.     int    21h        ;transfer to MS-DOS  (this time for good!  :D   )
  106.  
  107. select    endp           ;end of "select" procedure (which is the only one)
  108.  
  109.  
  110. prompt    db    'Choose an option please: ','$'   ;this is the prompt string
  111.                               ;that's printed in procedure
  112.                               ;select (see)
  113. lenval    db    0    ;this memory location stores the commad tail length
  114. valid    dw    0       ;this mem loc stores the start of the command tail
  115.  
  116.             ;both locations initialised to 0
  117.             ;note that these are not defined in a data segment
  118.             ;if a .COM file is intended
  119.  
  120. _TEXT    ends           ;end of "_TEXT" segment
  121.  
  122.     end    select  ;that's all folks. Start execution at procedure select
  123.  
  124.